package com.yiwei.zhh.api.requestInfo;

import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.AddResslistBean;
import com.yiwei.zhh.api.bean.InitAppBean;
import com.yiwei.zhh.api.bean.NewBeginApplyBean;
import com.yiwei.zhh.api.bean.NewV2BeginApplyBean;
import com.yiwei.zhh.api.bean.SubmitAlertBean;
import com.yiwei.zhh.api.callback.BeginApplyContract;
import com.yiwei.zhh.api.callback.SelectMoneyContract;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.model.AlertModel;
import com.yiwei.zhh.model.InitAppModel;
import com.yiwei.zhh.model.ResponseModel;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.PackageUtil;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.util.RBInfo;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * Created by Administrator on 2017/5/14.
 */

public class SelectMoneyRequest extends BaseRequest<SelectMoneyContract.View> implements SelectMoneyContract.Presenter<SelectMoneyContract.View> {

    public static SelectMoneyRequest create() {
        return new SelectMoneyRequest();
    }


    @Override
    public void getDealTypeInfo() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        Api.api_service.getDealTypes().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getDealTypeInfoSuccess(result);
                });
    }

    @Override
    public void getAlert() {
        if (!Network.checkNetwork(BaseContext)) {
            return;
        }
        Api.api_service.getAlert().compose(RxLifeUtil.NocheckOnDestroy(BaseContext)).
//                .doOnError(throwable -> {
//                    mView.showError();
//                })
//                .onErrorResumeNext(new Function<Throwable, ObservableSource<? extends AlertModel>>() {
//                    @Override
//                    public ObservableSource<? extends AlertModel> apply(Throwable throwable) throws Exception {
//                        throwable.printStackTrace();
//                        return Observable.empty();
//                    }
//                }).
        subscribe(result -> {
    mView.getAlertSuccess(result);
});
    }

    @Override
    public void submitAlert(String id) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        SubmitAlertBean bean = new SubmitAlertBean();
        bean.id = id;
        Api.api_service.submitAlert(bean).compose(RxLifeUtil.NoErrorOn(BaseContext))
                .doOnError(throwable -> {
                    mView.showError();
                })
                .onErrorResumeNext(new Function<Throwable, ObservableSource<? extends ResponseModel>>() {
                    @Override
                    public ObservableSource<? extends ResponseModel> apply(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                        return Observable.empty();
                    }
                }).
                subscribe(result -> {
                    mView.submitAlertSuccess();
                });
    }

    @Override
    public void addResslist(String deal_id, List<Map<String, String>> contactList, List<Map<String, String>> callLogList, List<Map<String, String>> appList, List<Map<String, String>> smsList) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        AddResslistBean item = new AddResslistBean();
        item.addresslist = contactList;
        item.app_list = appList;
        item.phone_call_records = callLogList;
        item.deal_id = deal_id;
        item.sms = smsList;
        Api.api_service.getAddResslistInfo(item).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.addResslistSuccess();
                });
    }

    @Override
    public void initApp() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        InitAppBean bean = new InitAppBean();
        bean.client_type = Const.CLIENT_TYPE;
        bean.device_id = RBInfo.getInstance().getUniquePsuedoID();
        bean.user_agent = RBInfo.getInstance().getUserAgent();
        bean.os = Const.DevType.ANDROID;
        bean.os_version = RBInfo.getInstance().getOSVersion();

        bean.browser = "";
        bean.browser_version = "";
        bean.mobile_brand = RBInfo.getInstance().getBrand();
        bean.mobile_model = RBInfo.getInstance().getModel();
        bean.app_version = PackageUtil.getVersionName(BaseContext);
        double lat = PreferenceUtils.getDouble(BaseContext.getApplicationContext(), Const.KEY.LAT, -1);
        double lng = PreferenceUtils.getDouble(BaseContext.getApplicationContext(), Const.KEY.LNG, -1);
        if (lat > 0 && lng > 0) {
            bean.lat = lat;
            bean.lang = lng;
        }


        Api.api_service.getInitInfo(bean).compose(RxLifeUtil.NoErrorOn(BaseContext))

                .doOnError(throwable -> {

                    mView.showError();
                })
                .onErrorResumeNext(new Function<Throwable, ObservableSource<? extends InitAppModel>>() {
                    @Override
                    public ObservableSource<? extends InitAppModel> apply(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                        return Observable.empty();
                    }
                }).
                subscribe(result -> {
                    mView.initAppSuccess(result);
                });
    }

    @Override
    public void getDealBefore() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        Api.api_service.getDealBeforeV3().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getDealBefore(result);
                });
    }

    @Override
    public void getOneKeyIdentity() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        Api.api_service.getCertificationBasics().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getOneKeyIdentitySuccess(result);
                });
    }
}
